import { Component, OnInit } from '@angular/core';
import { HeroService } from '../../../services/hero.service';
import { StorageService } from '../../../services/storage.service';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'app-tasklist',
  templateUrl: './tasklist.component.html',
  styleUrls: ['./tasklist.component.css']
})
export class TasklistComponent implements OnInit {

  loading = true;
  btn_loading=false;
  public id:number;
  public taskId:string='';
  public date:string='';
  public companyName:string='';
  public maint:string='';
  public status:string='';
  public userinfo:string;
  public maintinfo:string;
  public isVisible:boolean;
  public patchCheck:boolean=true;
  public maintlist:Array<any>=[];
  public patchcode:number;
  public patchValue:number;
  public tasklist:Array<any>=[];
  public PageIndex:number=1;
  public PageSize:number=10;
  public PageTotal:number;
  constructor(private heroService: HeroService,
              private message: NzMessageService,
              private modalService: NzModalService,
              private storageService: StorageService) { }

  ngOnInit() {
    this.getTasklist();
  }

  //获取任务工单列表数据
  getTasklist(){
    this.btn_loading=true;
    let url=host+"/main/merchantTaskOrder/list";
    let model="numberLike="+this.taskId+"&startDateBegin="+this.turnDate(this.date[0])+"&startDateEnd="+this.turnDate(this.date[1])+"&companyNameVague="+this.companyName+"&maintNameVague="+this.maint+"&status="+this.status+"&page="+this.PageIndex+"&pageSize="+this.PageSize;
    this.heroService.post(url,model).then(result=>{
      this.btn_loading=false;
      if(result.code==0){
        this.tasklist=result.data.pageBean.result;
        this.userinfo=result.data.connectUser;
        this.maintinfo=result.data.connectMaint;
        this.PageIndex=result.data.pageBean.curPage;  
        this.PageTotal=result.data.pageBean.totalRow;
        this.loading=false;
      }else{
        this.modalService.error({
          nzTitle: '提示',
          nzContent: result.msg
        });
      }
    })
  }

  //手动关闭工单
  closeTask(id){
    let url=host+"/main/merchantTaskOrder/close";
    let model="id="+id;
    this.heroService.post(url,model).then(result=>{
        if(result.code==0){
          this.message.success('工单确认完成成功');
          this.getTasklist();
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }
  //指派维修员
  patchMaint(id){
    this.id=id;
    this.isVisible = true;
    let url=host+"/main/util/getMaintByMerchantId";
    this.heroService.get(url).then(result=>{
        if(result.code==0){
          this.maintlist=result.data;
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }
  confirm(id): void {
    this.modalService.confirm({
      nzTitle: '提示',
      nzContent: '<p>当前工单已有维修员，确认重新指派维修员么？</p>',
      nzOnOk: () => this.patchMaint(id)
    });
  }
  handleOk(): void {
    if(this.patchValue){
      //this.patchCheck 禁止连续点击
      if(this.patchCheck){
        this.patchCheck=false;
        let url=host+"/main/merchantTaskOrder/dispatch";
        let model='id='+this.id+'&maintId='+this.patchValue;
        this.heroService.post(url,model).then(result=>{
            this.patchCheck=true;
            if(result.code==0){
              this.message.success('指派维修员完成');
              this.isVisible = false;
              this.getTasklist();
            }else{
              this.modalService.error({
                nzTitle: '提示',
                nzContent: result.msg
              });
            }
        })
      }
    }else{
      this.patchcode=-1;
    }
  }
  onFocus(){
    this.patchcode=0;
  }
  handleCancel(): void {
    this.isVisible = false;
  }
  //分页
  PageIndexChange(event){
    this.PageIndex=event;
    this.getTasklist();
  }

  //导出Excel
  exportOut() {
    const token = this.storageService.getItem("token");
    window.location.href = host + "/main/merchantTaskOrder/excelExport?token="+token+"&numberLike="+this.taskId+"&companyNameVague="+this.companyName+"&maintNameVague="+this.maint+"&status="+this.status;
  }

   //转时间格式
   turnDate(date) {
    if(date){
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let mydate = year.toString() + "-" + month.toString() + "-" + day.toString();
      return mydate;
    }
  }
}
